package com.chaos.framework.model.helper;


import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chaos.framework.model.dto.DTO;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryData;
import com.chaos.framework.model.dto.page.PageQueryDto;
import org.apache.commons.compress.utils.Lists;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Jacky
 * @date 2020/7/2 21:28
 */
public class PageHelper {
    public static <T> Page<T> page(Integer pageNum, Integer pageSize) {
        return new Page<>(pageNum, pageSize);
    }

    public static <T> Page<T> page(PageQueryData page) {
        return new Page<>(page.getPageNum(), page.getPageSize());
    }

    public static <T, R> PageList<R> pageList(IPage<T> page, Class<R> c) {
        List<R> list = ConvertHelper.convert(page.getRecords(), c);
        return PageList.<R>builder().current(page.getCurrent()).total(page.getTotal()).list(list).build();
    }

    public static <T> PageList<T> pageList(IPage<T> page) {
        List<T> list = page.getRecords();
        if (ArrayUtil.isEmpty(list)) {
            list = Lists.newArrayList();
        }

        return PageList.<T>builder().current(page.getCurrent()).total(page.getTotal()).list(list).build();
    }

    public static <T, R> PageList<R> pageList(IPage<T> page, Function<T, R> mapper) {
        List<T> records = page.getRecords();
        List<R> list = records.stream().map(mapper).collect(Collectors.toList());
        return PageList.<R>builder()
                .current(page.getCurrent())
                .total(page.getTotal())
                .list(list)
                .build();
    }

}
